package com.morphiastic;

import java.util.Iterator;

import com.google.code.morphia.query.Query;

/**
 * IndexWorker used to perform Query based update operations ( see updateFirst and update in morphia's  and morhpiastic DAO )
 * What this does is basically that the we execute the queryand iterate over the documents and then call indexEntity on ElasticStore
 * 
 * 
 * Note : this can potentially spawn a lot of small IndexWorkers careful with OutOfMemory ... 
 * TODO see if we can find a safer way
 * 
 * @author Alex
 *
 */
public class DefaultBulkUpdateIndexWorker implements IndexWorker{

	private IndexedClass indexedClass;
	private Query<?> query;
	private ElasticStore es;
	
	public DefaultBulkUpdateIndexWorker(IndexedClass indexedClass, Query<?> query, ElasticStore es){
		this.indexedClass = indexedClass;
		this.query = query;
		this.es = es;
	}
	
	@Override
	public void run() {
		Iterator<?> iterator = query.iterator();
		while (iterator.hasNext()) {
			Object object =  iterator.next();
			es.indexEntity(indexedClass,object);
		}
	}

}
